package com.x.crm.dao;

import com.x.crm.model.Mortgage;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class MortgageDao extends BaseDao {
    private final static String baseQuerySql = "SELECT * FROM CRM_MORTGAGE  T1 WHERE T1.MORTGAGE_ID > 0 ";
    public List<Mortgage> selectMortgageList(Mortgage mortgage) throws Exception {
        StringBuilder sql = new StringBuilder(baseQuerySql);
        List<Object> params = new ArrayList<>();
        if (this.isNotNull(mortgage.getMarketId())) {
            sql.append("AND T1.MARKET_ID = ? ");
            params.add(mortgage.getMarketId());
        }
        if (this.isNotNull(mortgage.getMortgageName())) {
            sql.append("AND T1.MORTGAGE_NAME = ? ");
            params.add(mortgage.getMortgageName());
        }
        if (this.isNotNull(mortgage.getMortgageId())) {
            sql.append("AND T1.MORTGAGE_ID = ? ");
            params.add(mortgage.getMortgageId());
        }
        if (this.isNotNull(mortgage.getShopId())) {
            sql.append("AND T1.SHOP_ID  = ? ");
            params.add(mortgage.getShopId());
        }
        sql.append(" ORDER BY T1.MORTGAGE_CREATE_TIME DESC ");
        return this.queryForList(sql.toString(), Mortgage.class, params, mortgage);
    }

    /**
     * 查询条数
     * @param Channel
     * @return
     * @throws Exception
     */
    private final static  String  selectMortgageCountSql = "SELECT COUNT(T1.MORTGAGE_ID) FROM  CRM_MORTGAGE T1 WHERE 1=1 ";
    public int selectMortgageNum(Mortgage mortgage) throws Exception{
        StringBuilder sql = new StringBuilder(selectMortgageCountSql);
        List<Object> params = new ArrayList<Object>();
        if (this.isNotNull(mortgage.getMarketId())) {
            sql.append("AND T1.MARKET_ID = ? ");
            params.add(mortgage.getMarketId());
        }
        if (this.isNotNull(mortgage.getMortgageName())) {
            sql.append("AND T1.MORTGAGE_NAME = ? ");
            params.add(mortgage.getMortgageName());
        }
        if (this.isNotNull(mortgage.getShopId())) {
            sql.append("AND T1.SHOP_ID  = ? ");
            params.add(mortgage.getShopId());
        }
        return this.queryForInt(sql.toString(), params);
    }

}